package mas.experiment_2_wordbook.diary;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.ArrayList;

import mas.experiment_2_wordbook.R;

public class Adapter extends ArrayAdapter<Diary> {

    private Context context;
    private int resource;
    private ArrayList<Diary> diaryList;

    public Adapter(@NonNull Context context, int resource, @NonNull ArrayList<Diary> objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
        this.diaryList = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        DiaryHolder holder = null;

        if (view == null) { //若是第一次加载页面则调用此方法初始化
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            view = inflater.inflate(resource, parent, false);

            holder = new DiaryHolder();
            holder.title = view.findViewById(R.id.diary_title);
            holder.date =  view.findViewById(R.id.diary_date);

            view.setTag(holder);
        } else {
            holder = (DiaryHolder) view.getTag();
        }
        //将数据传递给控件并显示
        Diary diary = getItem(position);
        holder.title.setText(diary.getTitle());
        holder.date.setText(diary.getDate());
        return view;
    }
    static class DiaryHolder {//在每个listview中显示标题和日期
        TextView title, date;
        //同时也显示图片
    }
}